(function(angular) {
	'use strict';
	angular.module('fs.waf').factory('nodeService', nodeService);
	nodeService.$inject = [ '$q', 'loggerFactory', 'commonService', 'ajaxServiceFactory' ];
	function nodeService($q, loggerFactory, commonService, ajaxServiceFactory) {
		var log = loggerFactory.getLogger("nodeService"), ajaxService = ajaxServiceFactory.buildAjaxService({
			getYesNo : {
				url : 'do/cda/dataList/yesno',
				method : 'get'
			},
			getAttrClass : {
				url : 'do/cda/dataList/getAttrClass',
				method : 'get'
			},
			getSiblingsForAlias : {
				url : 'do/cda/dataList/getSiblingsForAlias',
				method : 'post'
			}
		});
		
		function getYesNoOptions() {
			var defer = $q.defer();
			ajaxService.getYesNo().then(function(response) {
				defer.resolve(response);
			});
			return defer.promise;
		}
		function getSiblingsForAlias(limitNode) {
			var defer = $q.defer();
			ajaxService.getSiblingsForAlias(limitNode).then(function(response) {
				defer.resolve(response);
			});
			return defer.promise;
		}
		function getAttrClassOptions() {
			var defer = $q.defer();
			ajaxService.getAttrClass().then(function(response) {
				defer.resolve(response);
			});
			return defer.promise;
		}
		
		function initOptions(ctrl){
			ctrl.isAliasOptions = [];
			ctrl.yesNoOptions = [];
			ctrl.attrClassOptions = [];
			getYesNoOptions().then(function(response){
				ctrl.yesNoOptions = response.data;
				ctrl.isAliasOptions = response.data;
			});
			getAttrClassOptions().then(function(response){
				ctrl.attrClassOptions = response.data;
			});
			
			getSiblingsForAlias(ctrl.limitNode || {}).then(function(response){
				ctrl.siblingsOptions = response.data;
			});
			
			ctrl.isAliasChanged = function(value){
				if('Y' == value.isAlias){
					getSiblingsForAlias(ctrl.limitNode).then(function(response){
						ctrl.siblingsOptions = response.data;
					});
				}else{
					delete ctrl.siblingsOptions;
				}
			};
			
			ctrl.attrClassChanged = function(value){
				angular.forEach(ctrl.attrClassOptions, function(attr,index,array){
					if(attr.value == value.attrId){
						ctrl.limitNode.nodePath = value.parentNodePath + '/' + attr.label;
						ctrl.limitNode.limitNodeName = attr.label;
					}
				});
			};
		}
		return {
			initOptions : initOptions
		};
	}
})(angular);